﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace прямоугольнички
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        const int n=10;
        int state,q,num,m;
        bool flage;
        int[] mass,arr;
        Random rnd;
        const int H = 30;
        const int DL = 0;
        const int DH = 0;
        const int L = 30;
        private void numericUpDown1_ValueChanged(object sender, EventArgs e)
        {

        }

        private void pictureBox1_Paint(object sender, PaintEventArgs e)
        {
            Pen mypen=new Pen(Color.Black);
            Font myfont=new Font("Arial", 12);
            double LL=Math.Pow(2,Math.Truncate(Math.Log(n+1,2)))*L;
            SolidBrush myBrush1 = new SolidBrush(Color.Black);
            SolidBrush myBrush2 = new SolidBrush(Color.Red);
            SolidBrush myBrush3 = new SolidBrush(Color.Green);
            SolidBrush myBrush4 = new SolidBrush(Color.White);
            for (int i = 1; i <= n; i++)
            {
                double k = Math.Truncate(Math.Log(i, 2));
                int nu = i - (int)Math.Pow(2, k);
                int kol = (int)Math.Pow(2, k);
                e.Graphics.DrawRectangle(mypen, (float)(LL*nu/kol),(float)(H*k),(float)(LL/kol),(float)H);
                if (arr[i - 1] != mass[i - 1])
                {
                    arr[i - 1] = mass[i - 1];
                    e.Graphics.FillRectangle(myBrush4, (float)(LL * nu / kol), (float)(H * k), (float)(LL / kol), (float)H);
                }
                if ((i-1 != num && i != (2 * num + 2) && i != (2 * num + 3))|| state==4)
                {
                    if (i-1 < m || state < 3)
                        e.Graphics.DrawString(mass[i - 1].ToString(), myfont, myBrush1, (float)(LL * nu / kol), (float)(H * k));
                    else
                        e.Graphics.DrawString(mass[i - 1].ToString(), myfont, myBrush3, (float)(LL * nu / kol), (float)(H * k));
                }
                else
                {
                    e.Graphics.DrawString(mass[i - 1].ToString(), myfont, myBrush2, (float)(LL * nu / kol), (float)(H * k));
                }
                
            }
            
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            if (state == 0)
            {
                q = n - 1;
                state++;
                num = 0;
                return;
            }
            if(state==1)
            {
                num = q;
                pictureBox1.Invalidate();
                timer1.Enabled=false;
                timer2.Enabled=true;
                q--;
                if (q < 0)
                    state++;
                return;
            }
            if (state == 2)
            {
                pictureBox1.Invalidate();
                m = n;
                q = 0;
                state++;
                return;
            }
            if (state == 3)
            {
                //m--;
                int t = mass[m - 1];
                mass[m - 1] = mass[0];
                mass[0] = t;
                m--;
                flage = true;
                pictureBox1.Invalidate();
                num = 0;
                timer1.Enabled=false;
                timer2.Enabled=true;
                if (q == m)
                    state++;
                return;
            }
            if (state == 4)
            {
                m = 0;
                timer1.Enabled=false;
                timer2.Enabled = false;
                MessageBox.Show("Массив отсортирован", "Готова", MessageBoxButtons.OK, MessageBoxIcon.Information);
                pictureBox1.Invalidate();
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            pictureBox1.Visible = true;
            m = n;
            for (int i = 0; i < n; i++)
            {
                mass[i] = rnd.Next(100);
                arr[i] = mass[i];
            }
            pictureBox1.Width = n * 20 * L;
            pictureBox1.Height = n * H;
            timer1.Start();
            state = 0;
            timer2.Stop();
            pictureBox1.Invalidate();
        }

        private void timer2_Tick(object sender, EventArgs e)
        {
            flage = false;
            int a = 20000000;
            int b = 20000000;
            if ((2 * num + 1) < m)
                a = mass[2 * num + 1];
            if ((2 * num + 2) < m)
                b = mass[2 * num + 2];
            if (a <= b && a < mass[num] && a != 20000000)
            {
                int t = mass[2 * num + 1];
                mass[2 * num + 1] = mass[num];
                mass[num] = t;
                num = 2 * num + 1;
            }
            else
                if (b <= a && b < mass[num] && b != 20000000)
                {
                    int t = mass[2 * num + 2];
                    mass[2 * num + 2] = mass[num];
                    mass[num] = t;
                    num = 2 * num + 2;
                }
                else
                {
                    timer2.Enabled = false;
                    timer1.Enabled = true;
                }
            pictureBox1.Invalidate();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            pictureBox1.Visible = false;
            rnd = new Random();
            mass = new int[n];
            arr = new int[n];
        }
    }
}

